/**
 * 
 */
package com.platform.common.utils;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能：字符串工具类
 * 
 * @author gaowei
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
	
	public static final String EMPTY_STRING = "";
	private static char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static final char IntChars[] = "1234567890".toCharArray();
	
	/**
	 * <pre>
	 * 功能：获得int
	 * </pre>
	 * 
	 * @param val
	 * @return
	 */
	public static int getInt(String val) {
		return getInt(val, -1);
	}
	
	/**
	 * <pre>
	 * 功能：获得int
	 * </pre>
	 * 
	 * @param val
	 * @return
	 */
	public static int getInt(Object val) {
		if (val == null)
			return -1;
		return getInt(val.toString(), -1);
	}
	
	/**
	 * <pre>
	 * 功能：获得int
	 * </pre>
	 * 
	 * @param val
	 * @param def
	 * @return
	 */
	public static int getInt(Object val, int def) {
		try {
			return Integer.parseInt(val.toString().trim());
		} catch (Exception e) {
			return def;
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得long
	 * </pre>
	 * 
	 * @param val
	 * @return
	 */
	public static long getLong(String val) {
		return getLong(val, -1);
	}
	
	/**
	 * <pre>
	 * 功能：获得long
	 * </pre>
	 * 
	 * @param object
	 * @return
	 */
	public static long getLong(Object object) {
		return getLong(object, -1);
	}
	
	/**
	 * <pre>
	 * 功能：获得long
	 * </pre>
	 * 
	 * @param val
	 * @param def
	 * @return
	 */
	public static long getLong(Object val, long def) {
		try {
			if (val == null)
				return def;
			return Long.parseLong(val.toString().trim());
		} catch (Exception e) {
			return def;
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得boolean
	 * </pre>
	 * 
	 * @param val
	 * @return
	 */
	public static boolean getBoolean(String val) {
		return getBoolean(val, false);
	}
	
	/**
	 * <pre>
	 * 功能：获得boolean
	 * </pre>
	 * 
	 * @param val
	 * @param def
	 * @return
	 */
	public static boolean getBoolean(String val, boolean def) {
		if (ValidateUtils.isNull(val))
			return def;
		try {
			return Boolean.parseBoolean(val);
		} catch (Exception e) {
			return def;
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得double
	 * </pre>
	 * 
	 * @param value
	 * @return
	 */
	public static double getDouble(String value) {
		return getDouble(value, 0.00);
	}
	
	/**
	 * <pre>
	 * 功能：获得double
	 * </pre>
	 * 
	 * @param value
	 * @param def
	 * @return
	 */
	public static double getDouble(String value, double def) {
		try {
			return Double.valueOf(value).doubleValue();
		} catch (Exception e) {
			return def;
		}
	}
	
	/**
	 * <pre>
	 * 功能：替换字符串
	 * </pre>
	 * 
	 * @param text
	 * @param replaced
	 * @param replacement
	 * @return
	 */
	public static String replace(String text, String replaced, String replacement) {
		StringBuffer ret = new StringBuffer();
		String temp = text;
		
		while (temp.indexOf(replaced) > -1) {
			ret.append(temp.substring(0, temp.indexOf(replaced)) + replacement);
			
			temp = temp.substring(temp.indexOf(replaced) + replaced.length());
		}
		
		ret.append(temp);
		
		return ret.toString();
	}
	
	/**
	 * <pre>
	 * 功能：删除不能成为文件名的字符
	 * </pre>
	 * 
	 * @param fileStr
	 * @return
	 */
	public static String removeIllegalFileChars(String fileStr) {
		String replacedFileStr = fileStr;
		
		replacedFileStr = replace(replacedFileStr, " ", "_");
		replacedFileStr = replace(replacedFileStr, "&", EMPTY_STRING);
		replacedFileStr = replace(replacedFileStr, "%", EMPTY_STRING);
		replacedFileStr = replace(replacedFileStr, ",", EMPTY_STRING);
		replacedFileStr = replace(replacedFileStr, ";", EMPTY_STRING);
		replacedFileStr = replace(replacedFileStr, "/", "_");
		
		return replacedFileStr;
	}
	
	/**
	 * <pre>
	 * 功能：获得字符串中的指点子字符串
	 * </pre>
	 * 
	 * @param strIn
	 * @param start
	 * @param length
	 * @return
	 */
	public static String substring(String strIn, int start, int length) {
		String strOut = null;
		
		if (strIn != null) {
			strOut = EMPTY_STRING;
			
			if (start < strIn.length() && length > 0) {
				if (start + length < strIn.length()) {
					strOut = strIn.substring(start, start + length);
				} else {
					strOut = strIn.substring(start, strIn.length());
				}
			}
		}
		
		return strOut;
	}
	
	/**
	 * <pre>
	 * 功能：拼接字符串
	 * </pre>
	 * 
	 * @param objs
	 * @return
	 */
	public static String concat(Object... objs) {
		if (objs == null || objs.length < 1) {
			return EMPTY_STRING;
		}
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(obj == null ? EMPTY_STRING : obj.toString());
		}
		return sb.toString();
	}
	
	/**
	 * <pre>
	 * 功能：拼接字符串
	 * </pre>
	 * 
	 * @param objs
	 * @return
	 */
	public static String concat(String split, Object... objs) {
		if (objs == null || objs.length < 1) {
			return EMPTY_STRING;
		}
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			if (sb.length() != 0) {
				sb.append(split);
			}
			sb.append(obj == null ? EMPTY_STRING : obj.toString());
		}
		return sb.toString();
	}
	
	/**
	 * <pre>
	 * 功能：随机生成length位数字字符串
	 * </pre>
	 * 
	 * @param length
	 * @return
	 */
	public static final String getRandomLengthDigit(int length) {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < length; i++) {
			str.append(IntChars[(int) (Math.random() * 10)]);
		}
		return str.toString();
	}
	
	/**
	 * <pre>
	 * 功能：随即生成length位字符串
	 * </pre>
	 * 
	 * @param length
	 * @return
	 */
	public static final String getRandomLengthString(int length) {
		if (length < 1)
			return null;
		char ac[] = new char[length];
		for (int j = 0; j < ac.length; j++) {
			ac[j] = chars[new Random().nextInt(71)];
		}
		
		return new String(ac);
	}
	
	/**
	 * <pre>
	 * 功能：随即生成i位中文
	 * </pre>
	 * 
	 * @param length
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static final String getRandomLengthChineseString(int length) throws UnsupportedEncodingException {
		StringBuilder sb = new StringBuilder();
		for (int i = length; i > 0; i--) {
			sb.append(getRandomChinese());
		}
		return sb.toString();
	}
	
	/**
	 * <pre>
	 * 功能：随机产生中文,长度范围为start-end
	 * </pre>
	 * 
	 * @param start
	 * @param end
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getRandomLengthChiness(int start, int end) throws UnsupportedEncodingException {
		StringBuilder sb = new StringBuilder();
		int length = new Random().nextInt(end + 1);
		if (length < start) {
			return getRandomLengthChiness(start, end);
		} else {
			for (int i = 0; i < length; i++) {
				sb.append(getRandomChinese());
			}
		}
		return sb.toString();
	}
	
	/**
	 * <pre>
	 * 功能：随机获得中文
	 * </pre>
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getRandomChinese() throws UnsupportedEncodingException {
		String str = null;
		int highPos, lowPos;
		Random random = new Random();
		;
		highPos = (176 + Math.abs(random.nextInt(39)));
		lowPos = 161 + Math.abs(random.nextInt(93));
		byte[] b = new byte[2];
		b[0] = (new Integer(highPos)).byteValue();
		b[1] = (new Integer(lowPos)).byteValue();
		str = new String(b, "GB2312");
		return str;
	}
	
	/**
	 * <pre>
	 * 功能：删除额外的空格,保证一个空格
	 * </pre>
	 * 
	 * @param str
	 * @return
	 */
	public static String removeExtraBlanks(String str) {
		if (isBlank(str)) {
			return EMPTY_STRING;
		}
		char[] chars = str.toCharArray();
		int finalIndex = 0;
		int spaceCount = 0;
		for (int i = 0; i < chars.length; ++i) {
			if (chars[i] != ' ') {
				chars[finalIndex] = chars[i];
				++finalIndex;
				spaceCount = 0;
			} else if (chars[i] == ' ' && finalIndex == 0) {
			} else if (chars[i] == ' ' && spaceCount == 0) {
				chars[finalIndex] = chars[i];
				++finalIndex;
				++spaceCount;
			} else if (chars[i] == ' ' && spaceCount > 0) {
				++spaceCount;
			}
		}
		for (int i = finalIndex; i < chars.length; ++i) {
			chars[i] = ' ';
		}
		return String.valueOf(chars);
	}
	
	/**
	 * <pre>
	 * 功能：二行制转字符串
	 * </pre>
	 * 
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		StringBuilder hs = new StringBuilder();
		String stmp = EMPTY_STRING;
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs.append("0").append(stmp);
			} else {
				hs.append(stmp);
			}
		}
		return hs.toString().toUpperCase();
	}
	
	/**
	 * <pre>
	 * 功能：正则表达式替换
	 * </pre>
	 * 
	 * @param str
	 * @param pattern
	 * @param replacement
	 * @return
	 */
	public static String regexReplace(String str, String pattern, String replacement) {
		if (isBlank(str)) {
			return EMPTY_STRING;
		}
		if (isBlank(pattern)) {
			return str;
		}
		return str.replaceAll(pattern, replacement);
	}
	
	/**
	 * <pre>
	 * 功能：正则替换
	 * </pre>
	 * 
	 * @param str
	 * @param pattern
	 * @param replace
	 * @param connector
	 * @param def
	 * @return
	 */
	public static String regex(String str, String pattern, String replace, String connector, String def) {
		if (isBlank(str) || isBlank(pattern) || isBlank(connector)) {
			return EMPTY_STRING;
		}
		Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(str);
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (i = 0; matcher.find(); i++) {
			if (i > 0) {
				sb.append(connector);
			}
			matcher.appendReplacement(sb, replace);
		}
		if (i > 0) {
			matcher.appendTail(sb).append(def);
		} else {
			sb.append(str);
		}
		return sb.toString();
	}
	
	/**
	 * <pre>
	 * 功能：查找符合正则的表达式的字符串
	 * </pre>
	 * 
	 * @param str
	 * @param pattern
	 * @param index
	 * @return
	 */
	public static String regexFind(String str, String pattern, int index) {
		if (isBlank(str)) {
			return EMPTY_STRING;
		}
		Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(str);
		for (int i = 1; matcher.find(); i++) {
			if (i == index) {
				return matcher.group();
			}
		}
		return EMPTY_STRING;
	}
	
	/**
	 * <pre>
	 * 功能： 获得正则匹配的字符串
	 * </pre>
	 * 
	 * @param text
	 * @param pattern
	 * @param group
	 * @return
	 */
	public static String getMatcherGroup(String text, String pattern, int group) {
		if (ValidateUtils.isNotNull(text) && ValidateUtils.isNotNull(pattern)) {
			Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
			Matcher matcher = p.matcher(text);
			if (matcher.matches()) {
				return matcher.group(group);
			}
		}
		return null;
	}
	
	/**
	 * <pre>
	 * 功能：首字母大写
	 * </pre>
	 * 
	 * @param str
	 * @return
	 */
	public static String toUpperCaseFirstChar(String str) {
		if (isBlank(str)) {
			return str;
		}
		return new StringBuilder(str.length()).append(Character.toTitleCase(str.charAt(0))).append(str.substring(1)).toString();
	}
	
	/**
	 * <pre>
	 * 功能：首字母小写
	 * </pre>
	 * 
	 * @param str
	 * @return
	 */
	public static String toLowerCaseFirstChar(String str) {
		if (isBlank(str)) {
			return str;
		}
		return new StringBuilder(str.length()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
	}
	
	/**
	 * <pre>
	 * 功能：转义
	 * </pre>
	 * 
	 * @param data
	 * @return
	 */
	public static String escape(String data) {
		String[] escapes = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };
		
		if (!isEmpty(data)) {
			for (String escape : escapes) {
				if (data.contains(escape)) {
					data = data.replace(escape, MessageFormat.format("\\{0}", escape));
				}
			}
		}
		
		return data;
	}
}
